﻿/// -----------------------------------------------------------------------
/// (c) 2009 Microsoft Corporation -- All rights reserved
/// This code is licensed under the MS-PL
/// http://www.opensource.org/licenses/ms-pl.html
/// Courtesy of the Open Source Techology Center: http://port25.technet.com
/// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using gsToolkit;

namespace mkProject {
    class CompileCommandLine : CommandLine {
        public List<string> defines = new List<string>();
        public List<string> includes = new List<string>();
        public List<SourceFile> sourceFiles = new List<SourceFile>();
        public string targetOutput = "";
        public string outputPath = "";
        public string outputFile = "";

        public CompileCommandLine(string commandLine)
            : base(commandLine) {

            Regex regEx = new Regex(@"^[/-]D(.*)", RegexOptions.IgnoreCase);

            foreach (string cmdSwitch in switches) {
                MatchCollection matches = regEx.Matches(cmdSwitch);
                foreach (Match output in matches) {
                    defines.Add( output.Groups[1].Value );
                }
            }

            regEx = new Regex(@"^[/-]I(.*)", RegexOptions.IgnoreCase);

            foreach (string cmdSwitch in switches) {
                MatchCollection matches = regEx.Matches(cmdSwitch);
                foreach (Match output in matches) {
                    includes.Add(output.Groups[1].Value);
                }
            }

            // if there is an /fo switch, we know where this file is supposed to be output.
            regEx = new Regex(@"^[/-]fo\s*(.*)", RegexOptions.IgnoreCase);
            foreach (string cmdSwitch in switches) {
                MatchCollection matches = regEx.Matches(cmdSwitch);
                foreach (Match output in matches) {
                    targetOutput = output.Groups[1].Value;
                    if (targetOutput.EndsWith(".obj", StringComparison.CurrentCultureIgnoreCase) || targetOutput.EndsWith(".res", StringComparison.CurrentCultureIgnoreCase)) {
                        outputFile = targetOutput;
                        outputPath = System.IO.Path.GetDirectoryName(outputFile);
                    }
                    else {
                        // not a file? probably a target directory
                        outputPath = outputFile;
                    }
                    break;
                }
            }

            foreach (string p in parameters) {
                if (p.Match("\\.c$").Success || p.Match("\\.cpp$").Success || p.Match("\\.cxx$").Success || p.Match("\\.rc$").Success)
                    sourceFiles.Add(new SourceFile() { fileName = MakeProject.App.Redirect( p ), includes = includes, defines = defines, targetOutput = targetOutput, outputFile = outputFile, outputPath = outputPath });
            }
        }
    }
}

